import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

function traverseFolder(folderPath) {
  const files = fs.readdirSync(folderPath);

  files.forEach(file => {
    const filePath = path.join(folderPath, file);
    const fileStat = fs.statSync(filePath);

    if (fileStat.isDirectory()) {
      traverseFolder(filePath);
    } else {
      // 读取文件内容，替换Vue为Vue3,如果已经是Vue3,不用替换
      if (filePath.endsWith('.js')) {
        const fileContent = fs.readFileSync(filePath, 'utf-8');
        if (!/Vue3/g.test(fileContent)) {
          const replacedContent = fileContent.replace(/Vue/g, 'Vue3');
          fs.writeFileSync(filePath, replacedContent, 'utf-8');
        }
      }
    }
  });
}

traverseFolder(path.resolve(__dirname, '../pluginTemp/source'));
